package  net.siufung.market.base.user.provider;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.model.enums.DeletedEnum;
import net.siufung.core.utils.StringPool;
import net.siufung.market.base.dept.api.IDeptUserApi;
import net.siufung.market.base.user.mapper.IUserMapper;
import net.siufung.market.base.user.mapper.entity.User;
import net.siufung.market.base.user.service.IUserService;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * @author 陈建峰
 * @since 2022/8/1 10:52 上午
 */
@Slf4j
@AllArgsConstructor
public class DeptUserProvider implements IDeptUserApi {

    private final IUserMapper userMapper;
    private final IUserService userService;

    @Override
    public Boolean existUsedDept(String deptId) {
        return !Long.valueOf(StringPool.ZERO).equals(userMapper
                .selectCount(new LambdaQueryWrapper<User>().eq(User::getDeptId, deptId)));
    }

    @Override
    public Boolean existUsedDept(String[] deptIds) {
        return !Long.valueOf(StringPool.ZERO).equals(userMapper
                .selectCount(new LambdaQueryWrapper<User>().in(User::getDeptId, Lists.newArrayList(deptIds))));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateStatusByDeptIds(List<Serializable> deptIds, DeletedEnum deletedEnum) {
        userService.update(new LambdaUpdateWrapper<User>()
                .in(User::getDeptId, deptIds)
                .set(User::getDeleted, deletedEnum.getValue()));
    }
}
